#-*- codeing = utf-8 -*-
#@Time : 2020/10/30 16:26
#@Author : 阳某
#@File : 16.python绘制时间线轮播多图.py
#@Software : PyCharm


import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Pie, Bar, Timeline
df = pd.read_csv("./datas/beijing_tianqi/beijing_tianqi_2019.csv")
print(df.head(3))

df["month"] = pd.to_datetime(df["ymd"]).dt.month
print(df.head(3))
# 统计每个月份的每种天气出现次数
df_agg = df.groupby(["month", "tianqi"]).size().reset_index()
df_agg.columns = ["month", "tianqi", "count"]
print(df_agg.head(10))
# 怎样算出1月份的天气次数排名
df_agg[df_agg["month"]==1][["tianqi", "count"]].sort_values(by="count", ascending=False).values.tolist()

# 2. 按月变化-天气频率柱状图
timeline = Timeline()
timeline.add_schema(play_interval=1000)
for month in df_agg["month"].unique():
    data = (
        df_agg[df_agg["month"] == month][["tianqi", "count"]]
            .sort_values(by="count", ascending=True)
            .values.tolist()
    )

    # 绘制柱状图
    bar = Bar()

    # x轴是天气名称
    bar.add_xaxis([x[0] for x in data])
    # y轴是出现次数
    bar.add_yaxis("", [x[1] for x in data])
    # 让柱状图横放
    bar.reversal_axis()
    bar.set_series_opts(label_opts=opts.LabelOpts(position="right"))
    bar.set_global_opts(title_opts=opts.TitleOpts(title="北京每月天气变化"))

    timeline.add(bar, f"{month}月")

timeline.render_notebook()